package so.shanku.essential.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.widget.ImageView;
import android.widget.TextView;
import aym.util.getservicesdata.GetServicesDataQueue;
import aym.util.getservicesdata.GetServicesDataUtil;
import aym.util.json.JsonMap;
import aym.util.json.JsonParseHelper;
import com.nostra13.universalimageloader.core.ImageLoader;
import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.getdata.GetDataConfing;
import so.shanku.essential.getdata.GetDataQueue;
import so.shanku.essential.getdata.JsonKeys;
import so.shanku.essential.getdata.ShowGetDataError;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.utils.ImageLoaderBitmapCallBack;
import so.shanku.essential.view.WebViewNoScroll;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品售后服务详情页面
 * Created by Administrator on 2015/9/28.
 */
public class ProductAfterMarketDescActivity extends BaseUIActivity {
	/**
	 * 取消
	 */
	@ViewInject(id = R.id.tv_cancle, click = "tv_cancle_click")
	private TextView tv_cancle;
	/**
	 * 头部图片
	 */
	@ViewInject(id = R.id.iv_detail_top, click = "iv_detail_top_click")
	private ImageView iv_detail_top;

	/**
	 * 商品名
	 *
	 * @param savedInstanceState
	 */
	@ViewInject(id = R.id.tv_goods_name)
	private TextView tv_goods_name;

	/**
	 * 商品信息
	 *
	 * @param savedInstanceState
	 */
	@ViewInject(id = R.id.tv_goods_msg)
	private TextView tv_goods_msg;

	/**
	 * radiogroup中的详情
	 *
	 * @param savedInstanceState
	 */
	@ViewInject(id = R.id.tv_detail_goods, click = "tv_detail_goods_click")
	private TextView tv_detail_goods;
	/**
	 * radiogroup中的退换货
	 *
	 * @param savedInstanceState
	 */
	@ViewInject(id = R.id.tv_return_goods, click = "tv_return_goods_click")
	private TextView tv_return_goods;

	@ViewInject(id = R.id.tv_return_detail)
	private TextView tv_return_detail;
	/**
	 * radiogroup中的内容
	 *
	 * @param savedInstanceState
	 */
	@ViewInject(id = R.id.tv_detail_msg)
	private WebViewNoScroll tv_detail_msg;
	private String productId;
	private JsonMap<String, Object> jsonMap;//页面总数据

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_after_market_detail);
		productId = getIntent().getStringExtra(ExtraKeys.Key_Msg1);
		String shopName = getIntent().getStringExtra(ExtraKeys.Key_Msg2);
		setCenter_title(shopName);
		tv_detail_goods.setSelected(true);
		tv_return_goods.setSelected(false);
		getData_detailReturn();
		WebSettings webSettings=tv_detail_msg.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
		webSettings.setUseWideViewPort(false);//关键点

		webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

		webSettings.setDisplayZoomControls(false);
		webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本
		webSettings.setAllowFileAccess(true); // 允许访问文件
		webSettings.setBuiltInZoomControls(true); // 设置显示缩放按钮
		webSettings.setSupportZoom(true); // 支持缩放
		webSettings.setLoadWithOverviewMode(true);

	}

	public void iv_detail_top_click(View view) {
		finish();
	}

	public void tv_cancle_click(View view) {
		finish();
	}

	public void tv_detail_goods_click(View view) {
		setSelection(1);
	}

	public void tv_return_goods_click(View view) {
		setSelection(2);
	}

	public void getData_detailReturn() {
		loadingToast.show();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(GetDataQueue.Params_key, GetDataConfing.Key_Str);
		params.put("ProudcId", productId);
		GetDataQueue queue = new GetDataQueue();
		queue.setActionName(GetDataConfing.GetAction_GetProdctInfo);
		queue.setParamsNoJson(params);
		queue.setCallBack(callBack);
		queue.setWhat(GetDataConfing.what_GetProdctInfo);
		getDataUtil.getData(queue);
	}

	/**
	 * 获取服务器数据的返回信息
	 */
	private GetServicesDataUtil.IGetServicesDataCallBack callBack = new GetServicesDataUtil.IGetServicesDataCallBack() {
		@Override
		public void onLoaded(GetServicesDataQueue entity) {
			if (entity.isOk()) {
				if (ShowGetDataError.isOkAndCodeIsNot1(ProductAfterMarketDescActivity.this,
						entity.getInfo())) {
					if (entity.what == GetDataConfing.what_GetProdctInfo) {
						List<JsonMap<String, Object>> data = JsonParseHelper.getJsonMap_List_JsonMap(
								entity.getInfo(), JsonKeys.Key_Info);
						jsonMap = data.get(0);
						setData_return(jsonMap);
					}
				}
			} else {
				ShowGetDataError.showNetError(ProductAfterMarketDescActivity.this);
			}
			loadingToast.dismiss();

		}
	};

	public void setData_return(JsonMap data) {
		String brandLogo = data.getStringNoNull("ProductPic");
		if (MyApplication.getInstance().getBitmapHashMap().containsKey(brandLogo)) {
			iv_detail_top.setImageBitmap((MyApplication.getInstance().getBitmapHashMap().get(brandLogo)));
		} else {
			ImageLoader.getInstance().displayImage(brandLogo, iv_detail_top, MyApplication.getInstance().getDefaultOption(), new ImageLoaderBitmapCallBack());
		}
		tv_goods_name.setText(data.getStringNoNull("SalesName"));
        String dis_str=data.getStringNoNull("describe");
        if(TextUtils.isEmpty(dis_str)){
            tv_goods_msg.setVisibility(View.GONE);

        } else {
            tv_goods_msg.setText(dis_str);

        }
		setSelection(1);
	}

	public void setSelection(int i) {
		String url=GetDataConfing.Url_Base+"Product/AppReturnProductDetail?productId="+productId+"&flag="+i;
		if (i == 1) {
			tv_detail_goods.setSelected(true);
			tv_return_goods.setSelected(false);
//			tv_detail_msg.loadUrl(jsonMap.getStringNoNull("productIntro"));

//			tv_detail_msg.setVisibility(View.VISIBLE);
//			tv_return_detail.setVisibility(View.GONE);
		} else {
			tv_detail_goods.setSelected(false);
			tv_return_goods.setSelected(true);
			//tv_return_detail.setText(jsonMap.getStringNoNull("AfterService"));
//			tv_detail_msg.setVisibility(View.GONE);
//			tv_return_detail.setVisibility(View.VISIBLE);
		}

		tv_detail_msg.loadUrl(url);

		tv_detail_msg.setVisibility(View.VISIBLE);
		tv_return_detail.setVisibility(View.GONE);
	}
}
